-- 超级跳

local MissionBehaviorType = TaleWorlds.MountAndBlade.MissionBehaviorType
local AgentBuildData = TaleWorlds.MountAndBlade.AgentBuildData

local isFlying = false
local isSetMortalityState, lastMortalityState
local nextMenuId
local behavior

return {
onBeforeMissionBehaviorInitialize = function (mission)
    behavior = SubModule:CreateMissionBehavior({
        afterStart = function ()
            nextMenuId = Campaign.Current.GameMenuManager.NextGameMenuId
        end,
        onMissionTick = function (dt)
            local player = Agent.Main
            if player == nil or player.Health <= 0 then
                nextMenuId = nil -- 死了菜单就不管了
                return
            end

            local actionType = player:GetCurrentActionType(0)
            if Input.IsKeyPressed(InputKey.U) then
                isFlying = not isFlying

                local monster = player.Monster
                local jumpAcceleration = monster.JumpAcceleration
                if isFlying then
                    SubModule:SetPropertyValue(monster:GetType(), "JumpAcceleration", monster, 16, true)
                end

                local data = AgentBuildData(player.Origin)
                data:InitialFrameFromSpawnPointEntity(player.AgentVisuals:GetEntity())
                data:Equipment(player.SpawnEquipment)
                data:MissionEquipment(player.Equipment)
                data:Team(player.Team)
                data:Formation(player.Formation)
                data:Monster(monster)
                data:ClothingColor1(player.ClothingColor1)
                data:ClothingColor2(player.ClothingColor2)
                data:MissionPeer(player.MissionPeer)
                data:BodyProperties(player.BodyPropertiesValue)

                local newPlayer = mission:SpawnAgent(data, false)
                local oldPlayer = player

                if isFlying then
                    SubModule:SetPropertyValue(monster:GetType(), "JumpAcceleration", monster, jumpAcceleration, true)
                end

                newPlayer:FadeIn()
                newPlayer.Controller = Agent.ControllerType.Player
                oldPlayer.Controller = Agent.ControllerType.None
                oldPlayer:FadeOut(true, true)
            elseif isFlying and (actionType == Agent.ActionCodeType.Fall or actionType == Agent.ActionCodeType.JumpStart or actionType == Agent.ActionCodeType.Jump or
                                 actionType == Agent.ActionCodeType.JumpEnd or actionType == Agent.ActionCodeType.JumpEndHard) then
                if not isSetMortalityState then
                    lastMortalityState = player.CurrentMortalityState
                end
                player:SetMortalityState(Agent.MortalityState.Invulnerable)
                isSetMortalityState = true
            elseif isSetMortalityState and lastMortalityState ~= nil then
                player:SetMortalityState(lastMortalityState)
                isSetMortalityState = false
                lastMortalityState = nil
            end
        end,
        onAgentRemoved = function (affectedAgent, affectorAgent, agentState, killingBlow)
            behavior:SetTimeout(100, function ()
                if Campaign.Current.GameMenuManager.NextGameMenuId ~= nextMenuId then
                    Campaign.Current.GameMenuManager:SetNextMenu(nextMenuId)
                end
            end)
        end,
        onEndMission = function ()
            isFlying = false
            nextMenuId = nil
            lastMortalityState = nil
        end
    })
    mission:AddMissionBehavior(behavior)
end
}